﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace WindowsRemoteScripting.Action
{
    public class GuiHelper2
    {
        int viewRectWidth, viewRectHeight; // view window width and height

        Size canvasSize = new Size(60, 40);
        HScrollBar hScrollBar1;
        VScrollBar vScrollBar1;
        protected float zoom = 1.0f;
        
        /// <summary>
        /// Used to convert point to canvas - needed when zooming/scrolling
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        private Point PointToCanvas(Point point)
        {
            Point pt = new Point();
            if (viewRectWidth > canvasSize.Width * zoom)
            {
                pt.X = (int)((float)(point.X - viewRectWidth / 2 + canvasSize.Width * zoom / 2f) / zoom);
                pt.X = Math.Min(Math.Max(pt.X, 1), canvasSize.Width - 1);
            }
            else pt.X = (int)((float)(point.X + hScrollBar1.Value) / zoom);
            if (viewRectHeight > canvasSize.Height * zoom)
            {
                pt.Y = (int)((float)(point.Y - viewRectHeight / 2 + canvasSize.Height * zoom / 2f) / zoom);
                pt.Y = Math.Max(Math.Min(pt.Y, canvasSize.Height - 1), 1);
            }
            else pt.Y = (int)((float)(point.Y + vScrollBar1.Value) / zoom);
            return pt;
        }

        /// <summary>
        /// Used to Convert Point to Control - needed when zooming/scrolling
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        private Point PointToControl(Point point)
        {
            float xOffset = viewRectWidth > canvasSize.Width * zoom ? (viewRectWidth - canvasSize.Width * zoom) / 2f :
                -hScrollBar1.Value;
            float yOffset = viewRectHeight > canvasSize.Height * zoom ? (viewRectHeight - canvasSize.Height * zoom) / 2f :
                -vScrollBar1.Value;
            Matrix mxCanvastoContol = new Matrix();
            mxCanvastoContol.Scale(zoom, zoom);
            mxCanvastoContol.Translate(xOffset, yOffset, MatrixOrder.Append);
            Point[] pts = new Point[] { point };
            mxCanvastoContol.TransformPoints(pts);
            return pts[0];
        }
    }

    /// <summary>
    /// Some helper functions for the gui
    /// </summary>
    public static class GuiHelper
    {
        public static bool m_bOpenNewChildOnAction = true;
        public static string m_sFileName = string.Empty;
        public static bool m_bUseStandardFileDialog = true;

        public static bool m_bCloneAction = true;

        public static List<ICommand> m_aCommandList = new List<ICommand>(10);

        /// <summary>
        /// This is used to set the EventHandlers
        /// </summary>
        /// <param name="_item"></param>
        /// <param name="_icommand"></param>
        public static void SetEventHandler(ToolStripItem _item, ICommand _icommand)
        {
            _item.Tag = _icommand;
            _item.Enabled = true;
            Debug.WriteLine(_item.Name + " enabled");
            _item.Click += delegate(object sender, EventArgs e)
            {
                ExecuteAction(_item.Tag, EventArgs.Empty);
                //Debug.WriteLine(_icommand.Description);
            };
        }

        /// <summary>
        /// This is used to set the EventHandlers
        /// </summary>
        /// <param name="_item"></param>
        /// <param name="_icommand"></param>
        public static void SetEventHandler(Control _item, ICommand _icommand)
        {
            _item.Tag = _icommand;
            _item.Enabled = true;
            Debug.WriteLine(_item.Name + " enabled");
            _item.Click += delegate(object sender, EventArgs e)
            {
                ExecuteAction(_item.Tag, EventArgs.Empty);
                //Debug.WriteLine(_icommand.Description);
            };
        }

        public static void SetEventHandler()
        {
        }

        public static void SetEventHandler(string _sEventName, Control _item, ICommand _icommand)
        {
            _item.Tag = _icommand;
            _item.Enabled = true;
            Debug.WriteLine(_item.Name + " enabled");
            _item.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                ExecuteAction(_item.Tag, EventArgs.Empty);
                //Debug.WriteLine(_icommand.Description);
            };
        }

        /// <summary>
        /// CommandList
        /// </summary>
        public static List<ICommand> CommandList
        {
            get { return m_aCommandList; }
        }

        /// <summary>
        /// Executes a control EventHandler bound event-Action
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void ExecuteAction(object sender, EventArgs e)
        {
            object o = sender;

            ICommand command = sender as ICommand;
            if (command != null)
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();

                Debug.Write(string.Format("Executing command {0}", command.Description));

                Debug.WriteLine(string.Format("Execution time: {0}", watch.ElapsedTicks));

                command.Run(sender, e);

                Form1.m_Form.textBoxActionOutput.Text += 
                    command.ToString() +Environment.NewLine+
                    Form1.m_Form.textBoxScript.Text
                    + Environment.NewLine;

                //Set last command für redo
                ActionSingleton.LastCommand = command;

                //Clone action and add to command list
                ICommand icommand = null;
                if (m_bCloneAction)
                {
                    try
                    {
                        icommand = (ICommand)command.Clone();
                        m_aCommandList.Add(icommand);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine("Can't clone command!");
                    }
                }

                watch.Stop();

                //this.statusStrip1.Text = string.Format("Needed Time: {0}", watch.ElapsedTicks.ToString());
            }
        }
    }
}
